package com.hq.door.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hq.door.dao.TRecordsDAO;
import com.hq.door.dao.TStudentDAO;
import com.hq.door.dto.TRecordInfo;
import com.hq.door.pojo.TRecords;
import com.hq.door.pojo.TStudent;
import com.hq.door.service.TRecordsService;
import com.hq.door.util.POIUtil;

@Service("tRecordsService")
public class TRecordsServiceImpl extends BaseServiceImpl<TRecords> implements
		TRecordsService {
	@Autowired
	TRecordsDAO tRecordsDAO;
	@Autowired
	TStudentDAO tStudentDAO;

	public TStudentDAO gettStudentDAO() {
		return tStudentDAO;
	}

	public void settStudentDAO(TStudentDAO tStudentDAO) {
		this.tStudentDAO = tStudentDAO;
	}

	public TRecordsDAO gettRecordsDAO() {
		return tRecordsDAO;
	}

	@Autowired
	public void settRecordsDAO(TRecordsDAO tRecordsDAO) {
		this.setBaseDao(tRecordsDAO);
		this.tRecordsDAO = tRecordsDAO;
	}

	@Override
	public List<TRecordInfo> showAll(int type, String[] para, int pageStart,
			int pageSize) {

		String hql;
		List<TRecords> cords = new ArrayList<TRecords>();
		List<TStudent> tStudents = new ArrayList<TStudent>();
		List<TRecordInfo> infos = new ArrayList<TRecordInfo>();

		List<Object[]> results = new ArrayList<Object[]>();
		List<Object> conditions = new ArrayList<Object>();

		pageStart = (pageStart - 1) * pageSize;

		hql = "select tr,st from TRecords tr,TStudent st inner join st.TClass c inner join c.TMajor m inner join m.TInstitute ins where tr.cardNo=st.cordNo";

		for (int i = 0; i < para.length; i++) {
			if (!"".equals(para[i])) {
				hql = hql + " and";
				switch (i) {
				case 0:
					hql = hql + " ins.name=?";
					conditions.add(para[i]);
					break;
				case 1:
					hql = hql + " m.name=?";
					conditions.add(para[i]);
					break;
				case 2:
					hql = hql + " c.name=?";
					conditions.add(para[i]);
					break;
				case 3:
					hql = hql + " c.grade=?";
					conditions.add(para[i]);
					break;
				case 4:
					SimpleDateFormat df1 = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					try {
						Date date = df1.parse(para[i]);
						System.out.println(date);
						conditions.add(date);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					hql = hql + " tr.createTime>?";
					break;
				case 5:
					SimpleDateFormat df2 = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					try {
						conditions.add(df2.parse(para[i]));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					hql = hql + " tr.createTime<?";
					break;
				case 6:
					hql = hql + " tr.inoutType=?";
					conditions.add(Integer.parseInt(para[i]));
					break;
				case 7:
					hql = hql + " tr.isException=?";
					conditions.add(Integer.parseInt(para[i]));
					break;
				case 8:
					hql = hql + " (st.name like ? or tr.cardNo like ?)";
					conditions.add("%" + para[i] + "%");
					conditions.add("%" + para[i] + "%");
					break;
				default:
					break;
				}
			}
		}

		results = tRecordsDAO.findPageObjects(hql, pageStart, pageSize,
				conditions.toArray());

		for (int i = 0; i < results.size(); i++) {
			Object[] object = results.get(i);
			cords.add((TRecords) object[0]);
			tStudents.add((TStudent) object[1]);
		}

		infos = new TRecordInfo().getTOutrequest(cords);
		for (int i = 0; i < infos.size(); i++) {
			infos.get(i).setStudentName(tStudents.get(i).getName());
		}

		return infos;
	}

	@Override
	public int getCount(int type, String[] para) {
		String hql;
		int count = 0;
		List<Object> conditions = new ArrayList<Object>();

		hql = "select count(tr.id) from TRecords tr,TStudent st inner join st.TClass c inner join c.TMajor m inner join m.TInstitute ins where tr.cardNo=st.cordNo";

		for (int i = 0; i < para.length; i++) {
			if (!"".equals(para[i])) {
				hql = hql + " and";
				switch (i) {
				case 0:
					hql = hql + " ins.name=?";
					conditions.add(para[i]);
					break;
				case 1:
					hql = hql + " m.name=?";
					conditions.add(para[i]);
					break;
				case 2:
					hql = hql + " c.name=?";
					conditions.add(para[i]);
					break;
				case 3:
					hql = hql + " c.grade=?";
					conditions.add(para[i]);
					break;
				case 4:
					SimpleDateFormat df1 = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					try {
						conditions.add(df1.parse(para[i]));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					hql = hql + " tr.createTime>?";
					break;
				case 5:
					SimpleDateFormat df2 = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					try {
						conditions.add(df2.parse(para[i]));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					hql = hql + " tr.createTime<?";
					break;
				case 6:
					hql = hql + " tr.inoutType=?";
					System.out.println(para[i]);
					conditions.add(Integer.parseInt(para[i]));
					break;
				case 7:
					hql = hql + " tr.isException=?";
					System.out.println(para[i]);
					conditions.add(Integer.parseInt(para[i]));
					break;
				case 8:
					hql = hql + " (st.name like ? or tr.cardNo like ?)";
					conditions.add("%" + para[i] + "%");
					conditions.add("%" + para[i] + "%");
					break;
				default:
					break;
				}
			}
		}

		count = tRecordsDAO.getCount(hql, conditions.toArray());

		return count;
	}

	@Override
	public void excelExport(List<TRecordInfo> infos,
			HttpServletResponse response) {
		String title = "刷卡记录";
		String [] colName = {"序号","卡号","学生姓名","刷卡时间","出入校","备注"};
		List<Object []> dataList = new ArrayList<Object[]>();
		Object[] objs = null;
		for (TRecordInfo tRecord : infos) {
			objs = new Object[colName.length];
			objs[0] = tRecord.getId();
			objs[1] = tRecord.getCardNo();
			objs[2] = tRecord.getStudentName();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			if(tRecord.getCreateTime()!=null){
				objs[3] = df.format(tRecord.getCreateTime());
			}else {
				objs[3] = "未知时间";
			}
			objs[4] = tRecord.getInoutType()==0?"进校":"出校";
			objs[5] = tRecord.getIsException()==0?"正常出入":"异常出入";
			dataList.add(objs);
		}
		POIUtil poiUtil = new POIUtil(title, colName, dataList);
		try {
			poiUtil.export(response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
